package mest.service.hws.impl;

import com.alibaba.fastjson.JSON;
import mest.dao.hws.plan_workfinishMapper;
import mest.entity.plan_workfinish;
import mest.service.cxj.DictionariesOrderService;
import mest.service.hws.plan_productjob_workfinishService;
import mest.service.hws.plan_productjob_workfinish_workService;
import mest.service.hws.plan_workfinishService;
import mest.util.DateUtil;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 数据流计划完工表
 * */
@Service
public class plan_workfinishServiceImpl implements plan_workfinishService {

    @Resource
    private plan_workfinishMapper plan_workfinishMapper;

    @Resource
    private plan_productjob_workfinishService plan_productjob_workfinishService;

    @Resource
    private DictionariesOrderService dno;

    @Resource
    private plan_productjob_workfinish_workService plan_productjob_workfinish_workService;

    /*
     * 新增计划完工表
     * 时间，编码自动生成
     * 成功后返回数据，新增内容表
     * */
    @Override
    public String insert(Map<String,Object> record) {

        /*/insertModel /delModel /updateByone*/
        //System.out.println("进入增删改方法");
        int count = 0;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String theirDb = record.get("theirDb").toString();
            //新增编码
            String BM = ("SJLWGD" + dno.selectDictionariesOrder(record.get("theirDb").toString()).getPlanWorkfinishNumber());
            //System.out.println("新增表名是："+BM);
            //系统时间
            Date Time = DateUtil.getDateTimeToDate();
            //封装汇报表需要的类
            record.put("Time",Time);
            record.put("tableNumber",BM);
            //调用汇报表生成接口
            //System.out.println("进入汇报表新增");
            plan_workfinishMapper.insert(record);
            //System.out.println("新增的汇报表数据" + record.toString());
            //System.out.println("-----------------");

            //System.out.println("进入汇报表内容新增");
            //查询内容单需要的数据  输入poid
            List<Map<String, Object>> maps1 = plan_workfinishMapper.select_ppw(record.get("theirDb").toString(),record.get("poId").toString());
            //System.out.println("查询出汇报表内容新增的对象数组--------------------");
            //循环遍历 插入汇报表id，成为新的内容表对象数组
            for (Map<String, Object> ppw:maps1){
                ppw.put("pwId",record.get("id"));
                ppw.put("theirDb",record.get("theirDb"));           //添加数据库表
                //System.out.println("每一条数："+maps1);
            }
            //System.out.println("新增完工数据参数"+maps1.toString());            //-----------------有问题
            //新增内容表返回的对象数组
            plan_productjob_workfinishService.insert(maps1);
            //System.out.println("新增的完工内容数据，对象数组maps1"+maps1.toString());
            //System.out.println("-----------------");


            //System.out.println("进入汇报表内容下工序新增");
            //新增的完工内容数据有多少条就有多少个对象数组插入====================================工序新增暂时不用
           /* for (Map<String,Object> map2:maps1){
                System.out.println("map2"+map2.toString());
                //传入wrid查询完工内容下的工序所需要的信息 并返回对象数组
                List<Map<String, Object>> ppwwlist = plan_workfinishMapper.select_ppww(map2.get("theirDb").toString(),map2.get("wfId").toString());
                //查询出的对象数组
                System.out.println("查询出的工序对象数组"+ppwwlist.toString());
                //循环遍历工序需要的对象数组，插入sfid，pppwid，形成最总传入工序生成的参数
                    for (Map<String,Object> ppww1:ppwwlist){
                        ppww1.put("sfId",map2.get("sfId"));     //插入sfid
                        ppww1.put("ppwId",map2.get("id"));     //插入完工内容数据的id
                        ppww1.put("planNumber",map2.get("planNumber"));     //计划数量
                        ppww1.put("wrId",map2.get("wfId"));     //工艺版本
                    }
                    //得到最终参数
                System.out.println("最终新增工序的对象数组参数"+ppwwlist.toString());
                    //调用工序生成接口
                int count1  = plan_productjob_workfinish_workService.insert(record.get("theirDb").toString(), ppwwlist);
                count += count1;
                System.out.println("新增的完工内容下的工序，对象数组总数"+ppwwlist.toString());
                System.out.println("-----------------");
            }*/
            //System.out.println("-----------------");
            if(maps1.size()==0){
                map.put("code", 0);
                map.put("msg", "操作失败");
            }/*else if(count==0){
                map.put("code", 0);
                map.put("msg", "操作失败");
            }*/else {
                //操作成功后字典表+1
                dno.add_dictionaries_Order(theirDb,"planWorkfinishNumber");
                map.put("code", 0);
                map.put("msg", "操作成功,新增的完工汇报表：1条--" + "完工内容表：" + maps1.size() + "条--" + "完工内容工序表：" + count + "条");
            }
        } catch (Exception e) {
            map.put("code", 1);
            map.put("msg", "操作失败");
            e.toString();
        }
        return JSON.toJSONString(map);
    }

    //查询全部计划完工单
    @Override
    public Map<String,Object> selectAll(String theirDb,
                                        Integer limit,Integer page,Integer finishStatus){
        /* selectById  selectAll  */
        System.out.println("进入查询");
        Map<String,Object> map = new HashMap<String,Object>();
        try {
                map.put("code",0);
                map.put("msg","查询成功");
                map.put("count",plan_workfinishMapper.selectcount(theirDb,finishStatus));
                map.put("data",plan_workfinishMapper.selectAll(theirDb,limit,(page-1)*limit,finishStatus));
        }catch (Exception e){
                System.out.println(e.toString());
                map.put("code",1);
                map.put("msg","查询失败");
                map.put("errmsg",e.toString());
        }
        return map;

    }

    //根据id查询计划完工单
    @Override
    public String selectById(String theirDb,
                             Integer finishStatus){
        /* selectById  selectAll  */
        System.out.println("进入查询");
        Map<Object,Object> map = new HashMap<Object,Object>();
        try {
            map.put("code",0);
            map.put("msg","查询成功");
            map.put("count",plan_workfinishMapper.selectByPrimaryKey(theirDb,finishStatus));
            map.put("data",plan_workfinishMapper.selectByPrimaryKey(theirDb,finishStatus));
        }catch (Exception e){
            System.out.println(e.toString());
            map.put("code",1);
            map.put("msg","查询失败");
            map.put("errmsg",e.toString());
        }
        return JSON.toJSONString(map);

    }

    //更新完工汇报单  删除 更改状态 入库数量等
    @Override
    public String updateByone(String theirDb,
                              plan_workfinish record){
        /*/insertModel /delModel /updateByone*/
        System.out.println("进入增删改方法");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
                map.put("code", 0);
                map.put("msg", "操作成功");
                map.put("count", plan_workfinishMapper.updateByPrimaryKey(theirDb,record));
        }catch (Exception e){
            map.put("code", 1);
            map.put("msg", "操作失败");
            System.out.println(e.toString());
        }    
        return JSON.toJSONString(map);

    }

}
